/**
 * 华橙数字科技有限公司对外提供的三维客户端的云渲染模式的api js文件
 * 使用前请跟华橙数字科技有限公司协定好对接的方法接口
 * 目前仅支持以iframe的方式将云渲染网页嵌入到目标网页中
 * 支持纯js,vue,react框架
 */

/**
 * 单例提供api处理
 */

//uglifyjs ./src/qtWebBridge/api/HCApi.js  -o ./src//qtWebBridge/api/HCApi.min.js -c -m -b

//场景移动
export const OPERATE_MOVE_UP = 'move_up'; //上移
export const OPERATE_MOVE_DOWN = 'move_down'; //下移
export const OPERATE_MOVE_LEFT = 'move_left'; //左移
export const OPERATE_MOVE_RIGHT = 'move_right'; //右移
//场景选择
export const OPERATE_ROTATE_LEFT = 'rotate_left'; //左转
export const OPERATE_ROTATE_RIGHT = 'rotate_right'; //右转

export default class HCApiManager {
  static instance = null;

  constructor() {
    this.hcIframeEleID = null; //载入华橙云渲染页面的外部iframe
    this.messageReceiveFuncList = []; //华橙页面信息的接收方法
    this.isLogShow = true;    //是否显示log，默认显示
    this.prefix = 'HCMessage';
  }

  static getInstance() {
    if(HCApiManager.instance == null) {
      HCApiManager.instance = new HCApiManager();
    }
    return HCApiManager.instance;
  }

  /**
   * 初始化
   */
  init(hcIframeEleID) {
    this.hcIframeEleID = hcIframeEleID;
    this.fnDebugResponseFunc = null;
    let hcElement = this._checkState();
    if(hcElement) {
      window.addEventListener("message", this._receiveCallback.bind(this), false);
    }

    hcElement.onload = function() {
      let frameWidth = hcElement.clientWidth;
      let frameHeight = hcElement.clientHeight;
      this.pushMessageToHC('frameSize',[frameWidth,frameHeight]);
    }.bind(this);
  }

  /**
   * 设置是否显示log
   * @param isShow
   */
  setLogShow(isShow) {
    this.isLogShow = isShow;
  }

  /**
   * 设置debug回复的回调
   * @param fnDebugCallback
   */
  setDebugResponseFunc(fnDebugCallback) {
    this.fnDebugResponseFunc = fnDebugCallback;
  }

  /**
   * 添加接收方法
   * @param receiveFunc
   */
  addMessageReceiveFunc(receiveFunc) {
    this.messageReceiveFuncList.push(receiveFunc);
    if(this.isLogShow) {
      console.log(this.prefix,'添加接收方法:' + receiveFunc);
    }
  }

  /**
   * 移除接收方法
   * @param removeFunc
   */
  removeMessageReceiveFunc(removeFunc) {
    let index = this.messageReceiveFuncList.indexOf(removeFunc);
    if(index > -1) {
      this.messageReceiveFuncList.splice(index,1);
      if(this.isLogShow) {
        console.log(this.prefix,'移除接收方法:' + removeFunc);
      }
    }
  }

  /**
   * 发送消息到华橙页面
   * @param strFunc     协定好的方法名称
   * @param dataArray   协定好的数据数组,默认为空数组
   */
  pushMessageToHC(strKeyCode,dataArray = []) {
    let hcElement = this._checkState();
    if(hcElement) {
      let hcData = {};
      hcData.key = 'to_hc_message';
      hcData.code = strKeyCode;
      hcData.data = dataArray;
      hcElement.contentWindow.postMessage(hcData,'*');

      if(this.isLogShow) {
        console.log(this.prefix,'向HC发送消息--');
        console.log(this.prefix,strKeyCode);
        console.log(this.prefix,dataArray);
        console.log('**********************');
      }
    }
  }

  /**
   * 销毁
   */
  destroy() {
    window.removeEventListener("message", this._receiveCallback);
    this.messageReceiveFuncList = [];
  }

  ///////////////////////////////////////////////////////////////////
  /**
   * 检查状态
   * @private
   */
  _checkState() {
    let ele = document.getElementById(this.hcIframeEleID);
    if(ele == null) {
      throw('未找到载入华橙页面的Iframe，请确认是否初始化或iframe id是否正确！');
    }
    return ele;
  }

  /**
   * 接收华橙发送的消息
   * @private
   */
  _receiveCallback(messageEvent) {
    if(messageEvent.data != undefined) {
      if(messageEvent.data.key == 'from_hc_message') {
        //处理接收的消息
        let strCode = messageEvent.data.code;
        let data = messageEvent.data.data;
        for(let i in HCApiManager.getInstance().messageReceiveFuncList) {
          HCApiManager.getInstance().messageReceiveFuncList[i](strCode,data);
        }
        if(HCApiManager.getInstance().isLogShow) {
          console.log(HCApiManager.getInstance().prefix,'接收HC消息--');
          console.log(HCApiManager.getInstance().prefix,strCode);
          console.log(HCApiManager.getInstance().prefix,data);
          console.log('**********************');
        }
      }else if(messageEvent.data.key == 'response_command') {
        let strCode = messageEvent.data.code;
        let data = messageEvent.data.data;
        if(this.fnDebugResponseFunc) {
          this.fnDebugResponseFunc(strCode,data);
        }
      }
    }
  }
}
